package echat.future.cn.echat.message.activity;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.netease.nim.avchatkit.AVChatKit;
import com.netease.nim.avchatkit.AVChatUserData;
import com.netease.nim.avchatkit.activity.AVChatActivity;
import com.netease.nimlib.sdk.avchat.constant.AVChatType;

import java.util.ArrayList;

import echat.future.cn.echat.R;
import echat.future.cn.echat.message.MessageApi;
import echat.future.cn.echat.message.SwipeItemLayout;
import echat.future.cn.echat.message.adapter.AppointmentAdapter;
import echat.future.cn.echat.message.bean.AppointmentBean;
import echat.future.cn.echat.message.bean.AppointmentGourpBean;
import echat.future.cn.echat.my.bean.BaseVo;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import lumingweihua.future.cn.baselibgxh.base.BaseActivity;

/**
 * 我的预约
 * Created by JX on 2018/8/12.
 */

public class AppointmentActivity extends BaseActivity {

    private RecyclerView rv;

    private AppointmentAdapter mAdapter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_appointment);

        findViewById(R.id.iv_back).setOnClickListener(this);

        rv = findViewById(R.id.appointment_rv);
        mAdapter = new AppointmentAdapter(R.layout.message_appointment_rv_item, new ArrayList<>());
        mAdapter.bindToRecyclerView(rv);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        rv.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_start) {
                    AppointmentBean itemData = mAdapter.getData().get(position);
                    AVChatUserData avChatUserData = new AVChatUserData(
                            UserHelper.getUserId()
                            ,itemData.getMember_id()+""
                            , itemData.getAvatar()
                            , itemData.getUsername()
                            , "1"
                            , true
                            , itemData.getPrice()
                            ,0
                            ,true
                            ,(long)itemData.getDuration()
                            ,itemData.getAppoint_id()+"");
                    AVChatKit.outgoingCall(context,
                            itemData.getMobile(),
                            itemData.getUsername(),
                            AVChatType.VIDEO.getValue(),
                            AVChatActivity.FROM_INTERNAL,avChatUserData);
                }else if (view.getId() == R.id.tv_delete) {
                    delete(mAdapter.getData().get(position).getAppoint_id());
                }
            }
        });
    }

    @Override
    protected void initData() {
        new HttpUtilsRequst<AppointmentGourpBean>(this, HttpUtilsRequst.retrofitClient.create(MessageApi.class)
                .appointment_get(UserHelper.getToken()))
                .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<AppointmentGourpBean>() {
                    @Override
                    public void onSuccss(AppointmentGourpBean appointmentBeans) {
//                        mAdapter.setPrice(appointmentBeans.getPrice());

                        if(appointmentBeans.getPrice()>0){
                            for(AppointmentBean ab:appointmentBeans.getList()){
                                ab.setPrice(appointmentBeans.getPrice());
                            }
                        }
                        mAdapter.setNewData(appointmentBeans.getList());
                    }
                }).start();
    }

    private void delete(int appoint_id) {
        new HttpUtilsRequst<BaseVo>(this, HttpUtilsRequst.retrofitClient.create(MessageApi.class)
                .appointment_delete(UserHelper.getToken(), appoint_id))
                .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<BaseVo>() {
                    @Override
                    public void onSuccss(BaseVo appointmentBeans) {
                        initData();
                    }
                }).start();
    }

    @Override
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}